package com.FH.applyForm.officeServies.create_contact_group;

import com.FH.organization.email_contact.email_contactResoure;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/12/16.
 */
@Stateless
public class ContactGroupImp extends AccessDB implements ContactGroupDAO {
    @Inject
    email_contactResoure email_contactResoure;

    @Override
    public ResultDTO getContactGroupInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from contact_group where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("contact_groupid")) {
                //sql_w += " AND sys_menu_id = "+json.getString("sys_menu_id");
                sql_w += " AND contact_groupid ='" + json.getString("contact_groupid") + "'";
            }

            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String[]> seresult = new ArrayList<String[]>();

            List<ContactGroupShowlDTO> result = new ArrayList<ContactGroupShowlDTO>();

            if (!existName.equals("0")) {
                String sql = "select contact_groupid,name from contact_group where 1=1" + sql_w + sql_page;
                seresult = this.getReportingTo(sql);

                for (int i = 0; i < seresult.size(); i++) {

                    ContactGroupShowlDTO contactGroupShowlDTO = new ContactGroupShowlDTO();
                    contactGroupShowlDTO.setContact_groupid(seresult.get(i)[1]);
                    contactGroupShowlDTO.setName(seresult.get(i)[2]);

                    ResultDTO email_contacts = null;
                    if (seresult.get(i)[1] != null && !seresult.get(i)[1].equals("")) {
                        email_contacts = email_contactResoure.getemailContactInfo("{contact_groupid:" + seresult.get(i)[1] + "}", "1", "1");
                        contactGroupShowlDTO.setEmail_contacts(email_contacts.getValues());
                    }

                    result.add(contactGroupShowlDTO);
                }
            }

            if (seresult.size() != 0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Override
    public ResultDTO addContactGroupInfo(ContactGroupPullDTO contactGroupPullDTO) {
        int resultls = 0;
        ResultDTO resultDTO = new ResultDTO();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");


        String name = contactGroupPullDTO.getName();
        String sql = "select * from contact_group where name='" + name + "'";
        String sum = getReportingToOne(sql);
        if (!sum.equals("")) {

            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("此分组已存在，请重新分配");
            return resultDTO;
        }


        String sql_insert = "";
        sql_insert = "insert into contact_group (contact_groupid,name)" +
                "values('" + uuid + "','" + contactGroupPullDTO.getName() + "')";

        resultls = addReportingTo(sql_insert);
        if (resultls != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO updateContactGroupInfo(ContactGroupPullDTO contactGroupPullDTO) {
        return null;
    }

    @Override
    public ResultDTO deleteContactGroupInfo(ContactGroupPullDTO contactGroupPullDTO) {
        //删除之前查询一下该表是否有记录
        String sql = "select count(1) from contact_group where contact_groupid='" + contactGroupPullDTO.getContact_groupid() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        if (!existName.equals(0)) {

            String QUERY = "";
            QUERY += "delete from contact_group where contact_groupid='" + contactGroupPullDTO.getContact_groupid() + "'";

            result = this.addReportingTo(QUERY);
        }

        ResultDTO resultDTO = new ResultDTO();
        if (result != 0) {
            String update = "update email_contact set contact_groupid='(NULL)' where contact_groupid='" + contactGroupPullDTO.getContact_groupid() + "'";
            addReportingTo(update);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
